// JavaScript Document

$(document).ready(function() {
	$('.rosterRangsSortable').sortable({
		items : 'div',
		axis: 'y',
		containment: 'parent',
		tolerance: 'pointer',
		stop: function(event,ui){ rosterRangsSort(); }
	});
});

 function rosterPlayerCasual(h, idUser) {
	if(h.checked){
		$.post(
			"ajaxload.php",
			{ page: "modules/roster/admin/ajax.php", action: "rosterPersosSetCasual", idUser: idUser }
		);
	}	
	else{
		$.post(
			"ajaxload.php",
			{ page: "modules/roster/admin/ajax.php", action: "rosterPersosSetUnCasual", idUser: idUser }
		);
	}
 }

 function rosterRangsSort() {
	document.getElementById('rangs').value='';
	$('.rosterRangsSortable').find('div').each(function(){
		document.getElementById('rangs').value+=$(this).find('.nom').html()+':';
		if($(this).find(".ui-icon-minus").is('.ui-icon-minus')) document.getElementById('rangs').value+="0|";
		else  document.getElementById('rangs').value+="1|";
	});
	$.post('ajaxload.php', { page: 'modules/roster/admin/ajax.php', action: 'rosterRangs', rangs: document.getElementById('rangs').value });
}

 function rosterRangsToggle(h){
	if($(h).find(".ui-icon-minus").is('.ui-icon-minus')){
		$(h).find(".ui-icon-minus").addClass('ui-icon-check').removeClass('ui-icon-minus');
		rosterRangsSort();
	}else{
		$(h).find(".ui-icon-check").addClass('ui-icon-minus').removeClass('ui-icon-check');
		rosterRangsSort();
	}
 }
 
 function rosterRangsSupprimer(h){
	$(h).closest(".rosterRangsSortableItem").remove();
	rosterRangsSort();
 }
 
 function rosterRangsAjouter(){
	rang=document.getElementById('rosterRangsAjouter').value;
	document.getElementById('rosterRangsAjouter').value="";
	if(rang!=""){
		$('.rosterRangsSortable').append("<div class='rosterRangsSortableItem'><table cellspacing='0' cellpadding='1'><tr><td><span class='ui-icon ui-icon-arrowthick-2-n-s' style='float: left;'></span>&nbsp;<span class='nom'>"+rang+"</span></td><td style='width: 10px; border-width: 0px; border-left: 1px solid #000000;' onmouseover='$(this).addClass(\"ui-state-hover\");' onmouseout='$(this).removeClass(\"ui-state-hover\");' onclick='rosterRangsToggle(this);'><span class='ui-icon ui-icon-minus tooltip' title='Récupérer les membres de ce rang'></span></td><td style='width: 10px; border-width: 0px; border-left: 1px solid #000000;' onmouseover='$(this).addClass(\"ui-state-hover\");' onmouseout='$(this).removeClass(\"ui-state-hover\");'><span class='ui-icon ui-icon-circle-close tooltip' title='Supprimer ce rang'></span></td></tr></table></div>");
		set_tooltip(".rosterRangsSortableItem *[title].tooltip");
		rosterRangsSort() ;
	}
 }
 
 function rosterMAJ(){
	$("#rosterMAJboutonMAJ").addClass("ui-helper-hidden");
	$("#rosterMAJAlerte").show('drop',{direction: 'up'},750);
	$("#rosterMAJLog").prepend("<div id='recupGuilde'> - Récupération des informations de la guilde </div>");
	$.post('ajaxload.php', { page: 'modules/roster/admin/ajax.php', action: 'rosterMAJGuilde' },function(data){
		if(data!="nok"){
			$("#recupGuilde").append("<span class='green'> Ok</span>");
			//$("#rosterMAJLog").prepend(data);
			var str=data;
			var persos=str.split(",");
			$("#rosterMAJLog").prepend("<div> - "+persos.length+" persos à traiter </div>");
			if(persos.length>0){
				$("#rosterMAJProgress").progressbar({ value: 0 });
				scale=100/persos.length;
				var tmp= Array();
				for(i=0; i<persos.length; i++){
					perso=persos[i].split('-');
					$.post('ajaxload.php', { page: 'modules/roster/admin/ajax.php', action: 'rosterMAJPerso', nom: perso[0], rang: perso[1] },function(data){
						var perso2=data.split(",");
						tmp.push(perso2[0]);
						$("#rosterMAJProgress").progressbar('value',parseInt(scale*tmp.length));
						$("#rosterMAJLog").prepend("<div id='"+perso2[0]+"'> - "+perso2[0]+" </div>");
						if(perso2[1]=="ok") $('div #'+perso2[0]).append("<span class='green'> Ok</span>");
						else $('div #'+perso2[0]).append("<span class='red'> Erreur</span>");
					});
				}
			}
			/*$("#rosterMAJProgress").progressbar('value',100);
			$("#rosterMAJLog").prepend("<div> - Fin du traitement </div>");*/
		}else{
			$("#recupGuilde").append("<span class='red'> Erreur</span>");
			$("#rosterMAJLog").prepend("<div> - Arrêt du traitement </div>");
		}
	});
 }

function rosterPersosToggleUser(id){
	a=$("#rosterPersosUser"+id);
	b=$("#rosterPersosToggleUser"+id+"Button");
	if(a.css('display')=='none'){
		a.show("blind",{direction: "horizontal"},500);
		b.removeClass('ui-icon-circle-triangle-w');
		b.addClass('ui-icon-circle-triangle-e');
	}else{
		a.hide("blind",{direction: "horizontal"},500);
		b.removeClass('ui-icon-circle-triangle-e');
		b.addClass('ui-icon-circle-triangle-w');
	}
}

function rosterPersosChangeUser(id,h){
	idUser=h.options[h.selectedIndex].value;
	if(idUser!=0){
		$('#rosterPersosUser'+id+'Icon').removeClass('ui-icon-minus');
		$('#rosterPersosUser'+id+'Icon').addClass('ui-icon-person');
	}else{
		$('#rosterPersosUser'+id+'Icon').removeClass('ui-icon-person');
		$('#rosterPersosUser'+id+'Icon').addClass('ui-icon-minus');
	}
	$.post('ajaxload.php', { page: 'modules/roster/admin/ajax.php', action: 'rosterPersosChangeUser', idPlayer: id, idUser: idUser });
	rosterPersosToggleUser(id);
}

function rosterPersosAddPerso(){
	a=document.getElementById('rosterPersosAddPerso');
	if(a.value!=""){
		open_loading();
		window.open('admin?confmodule=roster&action=MAJPerso&perso='+a.value,'_self');
	}
}

 
 
 